import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapreduce.Job;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

public class WordCountExample {

    public static class Map implements Mapper<Object, Text, Text, IntWritable> {
        @Override
        public void map(Object object, Text value, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
            StringTokenizer stringTokenizer = new StringTokenizer(value.toString());
            while(stringTokenizer.hasMoreTokens()) {
                String token = stringTokenizer.nextToken();
                Text text = new Text(token);
                IntWritable intWritable = new IntWritable(1);
                outputCollector.collect(text, intWritable);
            }
        }

        @Override
        public void configure(JobConf jobConf) {
        }

        @Override
        public void close() throws IOException {
        }
    }

    public static class Reduce implements Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        public void reduce(Text text, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
            int sum = 0;
            for (Iterator<IntWritable> it = values; it.hasNext(); ) {
                IntWritable intWritable = it.next();
                sum += intWritable.get();
            }
            IntWritable intWritable = new IntWritable(sum);
            outputCollector.collect(text, intWritable);
        }

        @Override
        public void configure(JobConf jobConf) {

        }

        @Override
        public void close() throws IOException {

        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        //改用JobConf类原因是FileInputFormat.addInputPath只接受JobConf参数，Job是旧版写法
        JobConf job = new JobConf(conf, WordCountExample.class);
//        Job job = Job.getInstance(conf, "Data choice");
        job.setJobName("WordCountExample");
//        job.setJarByClass(WordCountExample.class);
        job.setMapperClass(WordCountExample.Map.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setReducerClass(WordCountExample.Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path("/test_work_2/input"));
        FileOutputFormat.setOutputPath(job, new Path("/test_work_2/output"));

        JobClient.runJob(job);
    }

}
